<template>

  <div class="app-container">
    <div class="common-content">
      <el-form label-width="80px">
        <el-form-item label="分类标题">
          <el-input v-model="title" maxlength="30" clearable placeholder="请输入标题"></el-input>
        </el-form-item>
        <el-form-item label="级别">
          <el-select v-model="tid" placeholder="请选择分类">
            <el-option label="顶级栏目" value="0"></el-option>
            <el-option v-for="item in categoryList" :key="item.tid" :label="item.html+item.typename" :value="item.tid"></el-option>
          </el-select>
        </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">添加</el-button>
            <el-button>取消</el-button>
          </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'addType',
    data() {
      return {
        categoryList: '',
        title:'',
        tid:'0'
      }
    },
    created(){
      this.getCategoryList()
    },
    methods:{
      async onSubmit(){
        if(this.title.trim()==''){
          this.$message.error('请输入标题')
          return
        }
        if(this.tid==''){
          this.$message.error('请选择栏目')
          return
        }
        const res = await this.$http('admin/insertNewType',{'title':this.title,'sid':this.tid})
        console.log(res)
        if(res.code==1){
          this.$message.success(res.info)
          setTimeout(()=>{
            this.$router.push('/articleType/typeList')
          },500)
        }else{
          this.$message.error(res.info)
        }
      },
      //获取分类数据
      async getCategoryList(){
        const res = await this.$http('admin/blogType',{
          'keyword':''
        })
        if(res.code==1){
          this.categoryList =res.data
        }else{
          this.$message.error(res.info)
        }
      },
    }
  }
</script>

<style scoped lang="scss">
  ::v-deep .el-input{
    width: 400px;
  }
  .editor-content {
    margin-top: 20px;
  }
</style>
